import request from "@/api/index";
interface dataSourceType {
  [propName: string]: any;
}
export const checkCreatSiteForm = async (_rule: dataSourceType = {}, value: any) => {
  if (!value && value != '0' && _rule.required) {// 必填项验证非空并排除非必须项
    return;
  }
  if (_rule.type === 'phone' && value) { // 没填值没有校验的必要
    const reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    if (!reg_tel.test(value)) {
      return Promise.reject(`${_rule.msg}`);
    } else {
      return Promise.resolve();
    }
  }

};

export const checkuserManage = async (_rule: dataSourceType = {}, value: any) => {
  console.log(_rule,value,'_rule')
  if (!value) {
    return Promise.reject(`${_rule.msg1}`);
  } else {
    var params = Object.assign(_rule.params, {
      orgName: value,
    })
    if (_rule.api) {
      let res = await request[`${_rule.api}`](params);
      if (!res) {
        return Promise.reject(`${_rule.msg2}`);
      } else {
        return Promise.resolve();
      }
    }

  }
};
export const checkOrgFaxForm = async (_rule: dataSourceType = {}, value: any) => {
  if (_rule.type === 'orgFax' && value) { // 没填值没有校验的必要
    const reg_Fax = /^[0-9]*$/;
    if (!reg_Fax.test(value)) {
      return Promise.reject(`${_rule.msg}`);
    } else {
      return Promise.resolve();
    }
  }

};
